//
//  DescribeViewController.m
//  ProfilePicture
//
//  Created by 张云江 on 2019/10/14.
//  Copyright © 2019年 张云江. All rights reserved.
//

#import "DescribeViewController.h"

#import "SDCycleScrollView.h"
#import "MainFunctionViewController.h"

@interface DescribeViewController () <UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

@implementation DescribeViewController

+ (DescribeViewController *)initDescribeViewController:(HomeInfoMode *)info
{
    DescribeViewController *vc = [[DescribeViewController alloc]init];
    vc.homeInfo = info;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)setNav
{
    self.customNavBar.title = [NSString stringWithFormat:@"预览"];
    self.customNavBar.backgroundColor = kUIColorFromRGB(0x2F54EB);
}

- (void)creatUI
{
    self.view.backgroundColor = kUIColorFromRGB(0xF3F5FF);
    
    NSArray *images = @[[UIImage imageNamed:@"Des_Banner_01"],[UIImage imageNamed:@"Des_Banner_02"],[UIImage imageNamed:@"Des_Banner_03"],[UIImage imageNamed:@"Des_Banner_04"],[UIImage imageNamed:@"Des_Banner_05"]];
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake((kScreenWidth - kRatio(252))/2,kNavigationHeight + kRatio(20), kRatio(252), kRatio(334)) imageNamesGroup:images];
    cycleScrollView.titlesGroup = @[@"身体直立，距离墙面20CM",
                                    @"建议他人帮助拍摄，便于剪裁",
                                    @"纯色墙做背景，避免与服装颜色相近",
                                    @"光线明亮，避免肤色不均",
                                    @"抬头挺胸平视前方，对齐参考线"];
    cycleScrollView.titleLabelTextAlignment = NSTextAlignmentCenter;
    
    cycleScrollView.showPageControl = NO;
    
    [self.view addSubview:cycleScrollView];
    cycleScrollView.layer.cornerRadius = 8;
    cycleScrollView.layer.masksToBounds = YES;
    cycleScrollView.backgroundColor = [UIColor clearColor];
    
    UIView *footBg = [[UIView alloc]init];
    [self.view addSubview:footBg];
    footBg.backgroundColor = [UIColor whiteColor];
    footBg.layer.cornerRadius = kRatio(26);
    footBg.layer.masksToBounds = YES;
    footBg.layer.shadowOffset = CGSizeMake(0,2);
    footBg.layer.shadowOpacity = 1;
    footBg.layer.shadowRadius = 8;
    [footBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(kRatio(26));
        make.height.mas_equalTo(kRatio(226 + 26));
    }];
    
    UILabel *des = [[UILabel alloc]init];
    des.text = [NSString stringWithFormat:@"照片尺寸"];
    des.textColor = kUIColorFromRGB(0x000000);
    des.font = [UIFont systemFontOfSize:kRatio(14)];
    [self.view addSubview:des];
    [des mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footBg).offset(kRatio(19));
        make.top.equalTo(footBg).offset(kRatio(20));
    }];
    
    UILabel *des1 = [[UILabel alloc]init];
    des1.text = [NSString stringWithFormat:@"冲洗尺寸:"];
    des1.textColor = kUIColorFromRGB(0x000000);
    des1.font = [UIFont systemFontOfSize:kRatio(14)];
    des1.alpha = 0.65;
    [self.view addSubview:des1];
    [des1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footBg).offset(kRatio(19));
        make.top.equalTo(des.mas_bottom).offset(kRatio(20));
    }];
    
    UILabel *size1 = [[UILabel alloc]init];
    size1.text = [NSString stringWithFormat:@"%@",_homeInfo.sizeDes];
    size1.textColor = kUIColorFromRGB(0x000000);
    size1.font = [UIFont systemFontOfSize:kRatio(14)];
    size1.alpha = 0.65;
    [self.view addSubview:size1];
    [size1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(footBg).offset(kRatio(-19));
        make.centerY.equalTo(des1.mas_centerY);
    }];
    
    UILabel *des2 = [[UILabel alloc]init];
    des2.text = [NSString stringWithFormat:@"像素尺寸:"];
    des2.textColor = kUIColorFromRGB(0x000000);
    des2.font = [UIFont systemFontOfSize:kRatio(14)];
    des2.alpha = 0.65;
    [self.view addSubview:des2];
    [des2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footBg).offset(kRatio(19));
        make.top.equalTo(des1.mas_bottom).offset(kRatio(20));
    }];
    
    UILabel *size2 = [[UILabel alloc]init];
    size2.text = [NSString stringWithFormat:@"%@",_homeInfo.sizeDes];
    size2.textColor = kUIColorFromRGB(0x000000);
    size2.font = [UIFont systemFontOfSize:kRatio(14)];
    size2.alpha = 0.65;
    [self.view addSubview:size2];
    [size2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(footBg).offset(kRatio(-19));
        make.centerY.equalTo(des2.mas_centerY);
    }];
    
    UILabel *des3 = [[UILabel alloc]init];
    des3.text = [NSString stringWithFormat:@"支持背景色:"];
    des3.textColor = kUIColorFromRGB(0x000000);
    des3.font = [UIFont systemFontOfSize:kRatio(14)];
    des3.alpha = 0.65;
    [self.view addSubview:des3];
    [des3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footBg).offset(kRatio(19));
        make.top.equalTo(des2.mas_bottom).offset(kRatio(20));
    }];
    
    UIView *iconBg = [[UIView alloc]init];
    [self.view addSubview:iconBg];
    [iconBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(footBg).offset(kRatio(-19));
        make.centerY.equalTo(des3.mas_centerY);
        make.width.mas_equalTo(kRatio(120));
        make.height.mas_equalTo(kRatio(50));
    }];
    
    NSMutableArray *arr_image = [NSMutableArray array];
    NSArray *titleArr = @[@"mf_change_bg_1",@"mf_change_bg_2",@"mf_change_bg_3",@"mf_change_bg_4",@"mf_change_bg_5"];
    for (NSString *str in titleArr) {
        UIImageView *imageV = [[UIImageView alloc]init];
        imageV.contentMode = UIViewContentModeScaleAspectFit;
        imageV.image = [UIImage imageNamed:str];
        [iconBg addSubview:imageV];
        [arr_image addObject:imageV];
    }
    
    [arr_image mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:kRatio(10) leadSpacing:kRatio(0) tailSpacing:kRatio(0)];
    
    [arr_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(iconBg.mas_centerY);
        make.width.height.mas_equalTo(kRatio(16));
    }];
    
    UIButton *exportBtn = [[UIButton alloc]init];
    [exportBtn setTitle:[NSString stringWithFormat:@"从相机导入"] forState:UIControlStateNormal];
    [exportBtn addTarget:self action:@selector(takeLibrary) forControlEvents:UIControlEventTouchUpInside];
    [exportBtn setBackgroundColor:kUIColorFromRGB(0x2F54EB)];
    [self.view addSubview:exportBtn];
    [exportBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth/2);
        make.bottom.left.equalTo(self.view);
        make.height.mas_equalTo(kRatio(50));
    }];
    
    UIButton *takingBtn = [[UIButton alloc]init];
    [takingBtn setTitle:[NSString stringWithFormat:@"立即拍照"] forState:UIControlStateNormal];
    [takingBtn addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
    [takingBtn setBackgroundColor:kUIColorFromRGB(0xFFBA64)];
    [self.view addSubview:takingBtn];
    [takingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kScreenWidth/2);
        make.bottom.right.equalTo(self.view);
        make.height.mas_equalTo(kRatio(50));
    }];
}

- (void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = NO;
        picker.sourceType = sourceType;
        [self presentModalViewController:picker animated:YES];
    }
}

- (void)takeLibrary
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = NO;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark - UIImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:@"public.image"]) {
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [picker dismissViewControllerAnimated:YES completion:nil];
        NSData *data = UIImageJPEGRepresentation(image,0.3);
        [self selectImage:data];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)selectImage:(NSData *)data
{
    WCDelegate.currSpecId = _homeInfo.specId;
    
    NSMutableDictionary *parametrs = [[NSMutableDictionary alloc]init];
    [parametrs setObject:[NSNumber numberWithInt:WCDelegate.currSpecId] forKey:@"spec_id"];
    [parametrs setObject:[NSNumber numberWithInt:_homeInfo.sizeW] forKey:@"width_px"];
    [parametrs setObject:[NSNumber numberWithInt:_homeInfo.sizeH] forKey:@"height_px"];
    [ZHProgressHUD showMessage:@"正在上传图片"];
    
    [[NetworkMgr shareInstance] uploadImage:data withApi:@"cut_check_pic" withParameters:parametrs success:^(NSDictionary * _Nonnull result, int code) {
        [ZHProgressHUD hideHUD];
        [MBProgressHUD hideHUD];
        NSDictionary *dic = [result objectForKey:@"result"];
        if (code == 200) {
            WCDelegate.currImage = data;
            CutCheckPicInfo *info = [[CutCheckPicInfo alloc] initWithDic:dic];
            MainFunctionViewController *vc = [MainFunctionViewController initWtihCutCheckPicInfo:info];
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            [ZHProgressHUD hideHUD];
            [MBProgressHUD showError:@"该图片无法进行制作！"];
            NSString *error = [result objectForKey:@"error"];
            NSLog(@"error = %@",error);
        }
    } failure:^(NSError * _Nonnull error) {
        [ZHProgressHUD hideHUD];
        [MBProgressHUD showError:@"上传图片失败！"];
    }];
}
@end
